﻿using Bandit.Helpers;
using Bandit.Sockets;
using Org.BouncyCastle.Asn1.Ocsp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ETC.DLEDLightingSystemPlus.Helpers
{
    
    public class HoneyWell3320TCPHelper
    {
        private Bandit.Sockets.BanditTcpClient _TcpClient;

        private System.Threading.AutoResetEvent _ARE = new System.Threading.AutoResetEvent(false);

        public delegate void TCPDataChangeHandler(object sender, string data);
        public event TCPDataChangeHandler TCPDataChange;

        public delegate void TCPConnectChangeHandler(object sender, bool isConncet);
        public event TCPConnectChangeHandler TCPConnectChange;

        protected string _ResponseData = string.Empty;
        /// <summary>
        /// 是否已连接
        /// </summary>
        public bool IsConnected
        {
            get
            {
                return _TcpClient == null ? false : _TcpClient.Connected;
            }
        }

        private bool Wait(int millisecondsTimeout)
        {
            _ARE.Reset();
            return _ARE.WaitOne(millisecondsTimeout);
        }

        private bool Set()
        {
            return _ARE.Set();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="onConnected"></param>
        /// <param name="onDisconnect"></param>
        public void Initialize(string IP, int Port)
        {
            _TcpClient = new Bandit.Sockets.BanditTcpClient(IP, Port, false);
            _TcpClient.IsIgnoreStickPackage = true;
            _TcpClient.OnConnected += delegate
            {
                LogHelper.Info("【Tcp Connected】", true);
                OnTcpConnectChange(this, true);
            };
            _TcpClient.OnDisconnect += delegate
            {
                LogHelper.Info("【Tcp Disconnect】", true);

                OnTcpConnectChange(this, false);

                _TcpClient.Connect();
            };
            _TcpClient.OnReceive += (sender, e) =>
            {
                OnTCPDataChange(sender, e.Message);

                _ResponseData = e.Message;

                Set();
            };
            _TcpClient.Connect();
        }

        public void disConnect()
        {

            _TcpClient.Disconnect(DisconnectCause.ClientDisconnect, "");

        }
        public void ReConnect()
        {
            _TcpClient.Disconnect(DisconnectCause.ClientDisconnect, "");

            _TcpClient.Connect();

        }
        public bool SendText(string text)
        {
            bool result = _TcpClient.SendText(text, true);

            Wait(3000);

            return result;
        }

        #region 指令集

        /// <summary>
        /// HELP 信息指令:"HELP"
        /// </summary>
        /// <returns></returns>
        public string GetHelpInfoCommand()
        {
            string Cmd = "HELP";

            this.SendText(Cmd);

            this.Wait(3000);

            return _ResponseData;
        }

        public string OpenScanBarcode()
        {
            _ResponseData = string.Empty;

            string Cmd = "16 54 0D 6F 01";

            this.SendText(Cmd);

            this.Wait(3000);

            return _ResponseData;
        }
        public string CloseScanBarcode()
        {
            _ResponseData = string.Empty;

            string Cmd = "16 55 0D";

            this.SendText(Cmd);

            this.Wait(3000);

            return _ResponseData;
        }

        #endregion

      
        // 调用事件函数
        public void OnTcpConnectChange(object sender,bool isConnect)
        {
            if(TCPConnectChange != null)
            {
                TCPConnectChange(sender, isConnect);
            }
        }
        public void OnTCPDataChange(object sender, string data)
        {
            if (TCPDataChange != null)
            {
                TCPDataChange(sender, data);
            }
        }
    }
}
